﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class WebCustomUserControls_SelectIdentifyNo : System.Web.UI.Page
{
    public int iCuSelect = 0;
    //private int iFlag = 0;//查询临时的识别码表
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //初始作废所有临时识别码表中数据
            ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
            Boolean bSuccess = cls_AutoGeneNoDAL.DeleteTempIdentifys();            
            //把传过来的参数放到隐藏框
            this.labhidPara.Text = Page.Request["ParBod_No"].ToString() + "*" + Page.Request["ParBod_Date"].ToString() + "*"
                + Page.Request["ParIm_No"].ToString() + "*" + Page.Request["ParBatch_no"].ToString();

            string [] arrPara = getParams();
            this.labQty.Value = Page.Request["ParQty"].ToString();//商品数量
            //this.labCuQty.Text = Page.Request["ParQty"].ToString();//商品数量

            //首先查询出库的识别码是否已经存在于公共识别码中。存在则显示出来
            DataSet dst = cls_AutoGeneNoDAL.GetExistIdentifyNos(arrPara[0],arrPara[2]);//出库单号、商品号  
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.dgrdIdentifyResult.DataSource = dst;
                this.dgrdIdentifyResult.DataBind();
                //保存到SESSION当中
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetQueryDataSet = dst;               
            }
            //否则查询该商品还未出库的识别码
            else
            {
                dst = cls_AutoGeneNoDAL.GetExistIdentifyNos("",arrPara[2]);//商品号  
                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    this.dgrdIdentifyResult.DataSource = dst;
                    this.dgrdIdentifyResult.DataBind();
                    //保存到SESSION当中
                    ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                    cls_Property.SetOrGetQueryDataSet = dst;            
                }
                else
                {
                    ////this.labMessage.Text = "<script language='javascript'>alert('该商品识别码未入库！');</script>";
                    this.labMessage.Text = "<script>alert('该商品识别码未入库！');window.close();</script>";
                }
            }             
        }
        this.labMessage.Text = "";

        //计算当前选中的项
        int iSum = 0;
        int iCount = this.dgrdIdentifyResult.Items.Count;
        for (int i = 0; i < iCount; i++)
        {
            CheckBox chkSel = (CheckBox)this.dgrdIdentifyResult.Items[i].FindControl("chkRsSelect");
            if (chkSel.Checked)
            {
                iSum++;
            }
        }
        this.labCuSelect.Text = iSum.ToString();
       
   }
            
    //作废当前选中的识别码
    protected void btnDel_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        string strCurIdentifyNo = this.hidCurIdenNo.Value.ToString().Trim();
        Boolean bSuccess = cls_AutoGeneNoDAL.DeleteCurrentIdentify(strCurIdentifyNo);      
    }
      
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdIdentifyResult.CurrentPageIndex;
            int intPageCounts = this.dgrdIdentifyResult.PageCount;

            //判断翻页方向            
            switch (strKey)
            {
                case "0":
                    intPageIndex = 0;
                    break;
                case "1":
                    if (intPageIndex > 0) intPageIndex -= 1;
                    break;
                case "2":
                    if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
                    break;
                default:
                    intPageIndex = intPageCounts - 1;
                    break;            
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    public void ChangPage()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = "0";
            int intPageIndex = this.dgrdIdentifyResult.CurrentPageIndex;
            int intPageCounts = this.dgrdIdentifyResult.PageCount;

            //判断翻页方向
            if (strKey == "0")
            {
                intPageIndex = 0;
            }

            if (intPageCounts > 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    private void BindQueryDataSet(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdIdentifyResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdIdentifyResult.DataSource = dst;
            this.dgrdIdentifyResult.DataBind();

            int intPageCount = this.dgrdIdentifyResult.PageCount;
            int intCurrPage = this.dgrdIdentifyResult.CurrentPageIndex + 1;
            this.lblQueryRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblQueryPageCounts.Text = "共" + intPageCount + "页";
            this.lblQueryCurrPage.Text = "当前第" + intCurrPage + "页";
            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = true;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = true;
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(78, 78, 78);

            if (intPageCount == 1)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage == intPageCount)
            {
                this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage - 1 == 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = false;
            }
        }
    }    

    //公共取传过来的参数(单号，日期，商品编号，批号 )
    private string[] getParams()
    {
        string getPara = this.labhidPara.Text.ToString().Trim();
        string[] arrPara = getPara.Split(new Char[] { '*' });
        return arrPara;
    }

    protected void dgrdIdentifyResult_ItemDataBound1(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CheckBox chkSel = (CheckBox)e.Item.FindControl("chkRsSelect");
            string iQty = this.labQty.Value.ToString();//商品数量
            //int i = 0;
            //int iPage = int.Parse(iQty)/10+1;&& dgrdIdentifyResult.CurrentPageIndex < iPage
            //默认选择当前的数量
            if (e.Item.ItemIndex < int.Parse(iQty) )
            {
                //CheckBox chkSel = (CheckBox)e.Item.FindControl("chkRsSelect");
                chkSel.Checked = true;
                iCuSelect++;
            }
            else
            {
                //CheckBox chkSel = (CheckBox)e.Item.FindControl("chkRsSelect");
                chkSel.Checked = false;
            }
            //chkSel.Attributes.Add("onclick", "selectIdentifyNo.submit()");
            this.labCuSelect.Text = iCuSelect.ToString();
        }
    }

    //识别码出库
    protected void btnOk_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        string iQty = this.labQty.Value.ToString();//商品数量
        Boolean bSuccess = false;
        int iSum = 0;
        string[] arrPara = getParams();
        int iCount = this.dgrdIdentifyResult.Items.Count;
        
        //判断返择的识别码数量是否等
        for (int i = 0; i < iCount; i++)
        {
            CheckBox chkSel = (CheckBox)this.dgrdIdentifyResult.Items[i].FindControl("chkRsSelect");
            if (chkSel.Checked)
            {
                iSum++;
            }
        }

        //处理识别码不足的情况
        if (iCount < int.Parse(iQty))
        {
            this.labMessage.Text = "<script>alert('识别码不足,请确认！');</script>";
        }
        else
        {
            if (iSum < int.Parse(iQty) || iSum > int.Parse(iQty))
            {
                this.labMessage.Text = "<script>alert('请选择同等数量的识别码！');</script>";
            }
            else
            {
                for (int i = 0; i < iCount; i++)
                {
                    CheckBox chkSel = (CheckBox)this.dgrdIdentifyResult.Items[i].FindControl("chkRsSelect");
                    if (chkSel.Checked)
                    {
                        bSuccess = cls_AutoGeneNoDAL.UpdatePubIdentifyNos(chkSel.ToolTip, arrPara[0], arrPara[1]);
                    }
                }
                if (bSuccess)
                {
                    this.labMessage.Text = "<script>alert('保存成功！');window.close();</script>";
                }
            }
        }
    }

    //查看已选识别码数量
    protected void btnSelQty_Click(object sender, EventArgs e)
    {
        this.btnSelQty.Attributes.Add("onclick", "selectIdentifyNo.submit()");
    }
}
